from PyQt5 import QtCore
from PyQt5.QtCore import Qt, QPoint, QPointF, QRectF
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QLabel


class MyLabel(QLabel):
    # 缩放比例的最小值和最大值
    SCALE_MIN_VALUE = 0.05
    SCALE_MAX_VALUE = 20.0

    def __init__(self, parent=None):
        super(MyLabel, self).__init__(parent)
        self.setMouseTracking(True)
        self.m_scaleValue = 0.25  # 缩放比例初始值
        self.m_rectPixmap = QRectF()
        self.m_scalePixmap = QRectF()
        self.m_drawPoint = QPointF(0, 0)
        self.m_pressed = False
        self.m_lastPos = QPoint()
        self.setMaximumSize(QtCore.QSize(1378, 922))

    def set_image(self, img):
        self.setPixmap(img)
        self.m_rectPixmap = QRectF(self.pixmap().rect())
        self.repaint()

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_pressed = True
            self.m_lastPos = event.pos()

    def mouseMoveEvent(self, event):
        if self.m_pressed:
            delta = event.pos() - self.m_lastPos
            self.m_lastPos = event.pos()
            self.m_drawPoint += delta
            self.repaint()

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_pressed = False

    def wheelEvent(self, event):
        if not self.m_rectPixmap.isEmpty():
            oldScale = self.m_scaleValue
            angle = event.angleDelta() / 8
            if angle.y() > 0:
                self.m_scaleValue *= 1.1
            else:
                self.m_scaleValue *= 0.9
            if self.m_scaleValue > self.SCALE_MAX_VALUE:
                self.m_scaleValue = self.SCALE_MAX_VALUE
            if self.m_scaleValue < self.SCALE_MIN_VALUE:
                self.m_scaleValue = self.SCALE_MIN_VALUE
            # 鼠标位在图片范围内，则以鼠标位置为中心缩放
            if self.m_scalePixmap.contains(event.pos()):
                x = self.m_drawPoint.x() - (event.pos().x() - self.m_drawPoint.x()) * (self.m_scaleValue / oldScale - 1)
                y = self.m_drawPoint.y() - (event.pos().y() - self.m_drawPoint.y()) * (self.m_scaleValue / oldScale - 1)
                self.m_drawPoint = QPointF(x, y)
            # 鼠标位置不在图片范围内，则以图片中心缩放
            else:
                x = self.m_drawPoint.x() - (self.m_rectPixmap.width() * (self.m_scaleValue - oldScale)) / 2
                y = self.m_drawPoint.y() - (self.m_rectPixmap.height() * (self.m_scaleValue - oldScale)) / 2
                self.m_drawPoint = QPointF(x, y)
            self.repaint()

    def paintEvent(self, event):
        if not self.m_rectPixmap.isEmpty():
            painter = QPainter(self)
            painter.setRenderHint(QPainter.SmoothPixmapTransform)
            scaled_pixmap = self.pixmap().scaled(self.pixmap().size() * self.m_scaleValue, Qt.KeepAspectRatio,
                                                 Qt.FastTransformation)
            self.m_scalePixmap = QRectF(scaled_pixmap.rect())
            painter.drawPixmap(self.m_drawPoint, scaled_pixmap)

    def mouseDoubleClickEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_drawPoint = QPoint(0, 0)
            self.m_scaleValue = 0.25
            self.repaint()



